const express=require('express');
const pool=require('../pool.js')
const router=express.Router();

router.get('/index',(req,res,next)=>{
  pool.query('select * from indexs',(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.length){
        res.send({code:200,data:result[0]})
      }else{
        res.send({code:201,msg:'数据获取失败，请检查您的网络'})
      }
    }
  })
})
router.get('/types',(req,res,next)=>{
  pool.query('select * from types',(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.length){
        res.send({code:200,data:result})
      }else{
        res.send({code:201,msg:'数据获取失败，请检查您的网络'})
      }
    }
  })
})
router.get('/products',(req,res,next)=>{
  // console.log(req.query);
  pool.query('select * from products where tid=?',req.query.tid,(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.length){
        res.send({code:200,data:result})
      }else{
        res.send({code:201,msg:'数据获取失败，请检查您的网络'})
      }
    }
  })
})
router.get('/sreach',(req,res,next)=>{
  // console.log(req.query);
  pool.query(`select * from products where name like "%${req.query.name}%"`,(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.length){
        res.send({code:200,data:result})
      }else{
        res.send({code:201,msg:'数据获取失败，请检查您的网络'})
      }
    }
  })
})


module.exports=router;